package com.zhongjiananbang.textdemo.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhongjiananbang.textdemo.R;
import com.zhongjiananbang.textdemo.RecyclerViewListener;

import java.util.List;

/**
 * Created by renjibo on 2017/11/2.
 */

public class LeftAdapter extends RecyclerView.Adapter<LeftAdapter.LeftViewHolder> {

    private Context context;
    private List<String> listLeft;
    private final LayoutInflater inflater;
    private RecyclerViewListener recyclerViewListener;
    private View oldeView;
    private boolean isLeft;
    public LeftAdapter(Context context, List<String> listLeft,boolean isLeft){
        this.isLeft=isLeft;
        this.context=context;
        this.listLeft=listLeft;
        inflater = LayoutInflater.from(context);
    }
    @Override
    public LeftViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new LeftViewHolder(inflater.inflate(R.layout.left_item,parent,false));

    }
    public void setRecyclerViewListener(RecyclerViewListener recyclerViewListener){
        this.recyclerViewListener=recyclerViewListener;
    }
    @Override
    public void onBindViewHolder(final LeftViewHolder holder, final int position) {
        if(isLeft)
        holder.leftItem_linear.setBackgroundColor(Color.GRAY);

            holder.leftItem_textView.setText(listLeft.get(position)+"");
            holder.leftItem_linear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(isLeft){
                        if(oldeView!=null){
                            oldeView.setBackgroundColor(Color.GRAY);
                        }
                        holder.leftItem_linear.setBackgroundColor(Color.WHITE);
                        oldeView=holder.leftItem_linear;
                    }
                    Log.d("zzzzzzzzzz", "onClick:点击了");
                    if(recyclerViewListener!=null){
                        recyclerViewListener.onItemListner(position);
                    }
                }
            });
    }

    @Override
    public int getItemCount() {
        return listLeft.size();
    }

    class LeftViewHolder extends RecyclerView.ViewHolder{

        private final TextView leftItem_textView;
        private final LinearLayout leftItem_linear;

        public LeftViewHolder(View itemView) {
            super(itemView);
            leftItem_textView = itemView.findViewById(R.id.leftItem_textView);
            leftItem_linear = itemView.findViewById(R.id.leftItem_linear);
        }
    }
}
